package com.parking.carsystem.parkingchargesystem.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.GroupListModule;
import com.parking.carsystem.parkingchargesystem.module.InvoiceMessageTitle;

import java.util.ArrayList;

import static com.parking.carsystem.parkingchargesystem.common.Constant.VIEWTYPE_OTHER;

public class InvoiceMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public ArrayList<InvoiceMessageTitle.DataBean> dataBeans;

    public Activity activity;
    public boolean choose;

    public InvoiceMessageAdapter(ArrayList<InvoiceMessageTitle.DataBean> dataBeans, Activity activity, boolean choose) {
        this.dataBeans = dataBeans;
        this.activity = activity;
        this.choose = choose;
    }

    @Override
    public int getItemViewType(int position) {
        InvoiceMessageTitle.DataBean dataBean = dataBeans.get(position);
        if (dataBean.type.equals("01")||dataBean.type.equals("03")) {
            //企业和个人
            return Constant.VIEWTYPE_THREE;
        } else if (dataBean.type.equals("10")) {
            //提示
            return Constant.VIEWTYPE_OTHER;
        }
        return Constant.VIEWTYPE_OTHER;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == Constant.VIEWTYPE_THREE) {
            return new ParkingViewHodler(LayoutInflater.from(activity).inflate(R.layout.item_list_invoice, parent, false));
        } else {
            return new AddViewHodler(LayoutInflater.from(activity).inflate(R.layout.item_list_invoice_type, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        InvoiceMessageTitle.DataBean dataBean = dataBeans.get(position);
        if (dataBean.type.equals("01")||dataBean.type.equals("03")) {
            ParkingViewHodler parkingViewHodler = (ParkingViewHodler) holder;
            parkingViewHodler.text.setText(dataBean.name);
            if(choose){
                parkingViewHodler.choose_image.setVisibility(View.VISIBLE);
                if(dataBean.choose){
                    parkingViewHodler.choose_image.setImageResource(R.mipmap.selected);
                }else {
                    parkingViewHodler.choose_image.setImageResource(R.mipmap.unselected);
                }
            }else {
                parkingViewHodler.choose_image.setVisibility(View.GONE);
            }
        }else {
            AddViewHodler addViewHodler = (AddViewHodler) holder;
            addViewHodler.company_title.setText(dataBean.name);
        }
    }

    @Override
    public int getItemCount() {
        return dataBeans == null ? 0 : dataBeans.size();
    }

    public void setDatas(ArrayList<InvoiceMessageTitle.DataBean> dataBeans) {
        this.dataBeans = dataBeans;
        this.notifyDataSetChanged();
    }

    static class AddViewHodler extends RecyclerView.ViewHolder {
        TextView company_title;

        public AddViewHodler(View itemView) {
            super(itemView);
            company_title = itemView.findViewById(R.id.company_title);
        }
    }

    static class ParkingViewHodler extends RecyclerView.ViewHolder {
        TextView text;
        ImageView choose_image;

        public ParkingViewHodler(View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
            choose_image = itemView.findViewById(R.id.choose_image);
        }
    }
}
